unit uMsgControl;

interface
uses uAbstractControl,Classes;

  type
    TMsgControl=class (TAbstractControl)
      public
        procedure PostNotification(email:string;Text:string);
        class function GetInstance:TMsgControl;
        procedure CreateDefaultForm;override;
        procedure CreateDefaultDM;override;
      strict private
        class var
          FInstance:TMsgControl;
    end;
implementation
uses
  Forms,SqlExpr,SysUtils;


{ TMsgControl }

procedure TMsgControl.CreateDefaultDM;
begin
  inherited;

end;

procedure TMsgControl.CreateDefaultForm;
begin
  inherited;
end;

class function TMsgControl.GetInstance: TMsgControl;
begin
  if FInstance=nil then
  begin
     FInstance:=uMsgControl.TMsgControl.Create(Application);
  end;
  Result:=FInstance;
end;

procedure TMsgControl.PostNotification(email, Text: string);
var
  sqlPost:TSQLQuery;
  bLocalTrans:boolean;
begin
  sqlPost:=TSQLQuery.Create(nil);
  try
    sqlPost.SQLConnection:=DBConnection;
    {INSERT INTO MSLOG (  MSLOG_ID,  EMAIL,  MSG,  SENT) VALUES(0,'L','L','L')}
    sqlPost.SQL.Add('INSERT INTO MSLOG (');
    sqlPost.SQL.Add('  MSLOG_ID,');
    sqlPost.SQL.Add('  EMAIL,');
    sqlPost.SQL.Add('  MSG,');
    sqlPost.SQL.Add('  SENT');
    sqlPost.SQL.Add(') VALUES(');
    sqlPost.SQL.Add('0,');
    sqlPost.SQL.Add(QuotedSTR(email)+',');
    sqlPost.SQL.Add(QuotedSTR(Text)+',');
    sqlPost.SQL.Add(QuotedSTR('N')+')');

    bLocalTrans:=false;
    if not DBConnection.InTransaction then
    begin
      bLocalTrans:=True;
      DBConnection.Commit(TD);
    end;
    try
        sqlPost.ExecSQL(True);
        if(bLocalTrans) then
          DBConnection.Commit(TD);
    except
      on E: Exception do
      begin
          if(bLocalTrans) then
            DBConnection.Rollback(TD);
          raise Exception.Create(MO_ExceptionMsg+E.Message);
        end;
      end;

  finally
    sqlPost.Free;
  end;
end;

end.
